package org.jeecg.modules.xindian.reconciliation.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import org.jeecg.modules.xindian.reconciliation.controller.req.ReportManagerReq;
import org.jeecg.modules.xindian.reconciliation.controller.req.SettlementOne;
import org.jeecg.modules.xindian.reconciliation.entity.*;
import org.jeecg.modules.xindian.reconciliation.enums.RelationType;
import org.jeecg.modules.xindian.reconciliation.mapper.PolicySettlementStatisticsMapper;
import org.jeecg.modules.xindian.reconciliation.service.IChannelSettlementConfigService;
import org.jeecg.modules.xindian.reconciliation.service.IChannelSettlementInfoService;
import org.jeecg.modules.xindian.reconciliation.service.IPolicySettlementStatisticsService;
import org.jeecg.modules.xindian.reconciliation.service.ISalesmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: policy_settlement_statistics
 * @Author: jeecg-boot
 * @Date: 2024-05-06
 * @Version: V1.0
 */
@Service
public class PolicySettlementStatisticsServiceImpl extends ServiceImpl<PolicySettlementStatisticsMapper, PolicySettlementStatistics> implements IPolicySettlementStatisticsService {
    @Autowired
    private ISalesmanService iSalesmanService;
    @Autowired
    private IChannelSettlementConfigService channelSettlementConfigService;

    @Override
    public void settlementOne(SettlementOne one) {
        ReportManagerReq.Type type = ReportManagerReq.Type.valueOf(one.getType());
        PolicySettlementStatistics statistics = getById(one.getId());
        Assert.notNull(statistics, "结算单信息{}不存在,请重新选择", one.getId());
        switch (type) {
            case SALESMAN: {
                Salesman salesman = iSalesmanService.getById(one.getSalesmanId());
                Assert.notNull(salesman, "业务员不存在,请重新选择");
                statistics.setSalesmanFeeRate(one.getSalesmanRate());
                statistics.setSalesmanFee(statistics.getExTaxPremium().multiply(one.getSalesmanRate()).setScale(2, RoundingMode.HALF_UP));
                statistics.setSalesmanId(salesman.getId());
                statistics.setSalesmanName(salesman.getName());
                statistics.setChannelFeeRate(BigDecimal.ZERO);
                statistics.setChannelFee(BigDecimal.ZERO);
                statistics.setChannelAloneFee(BigDecimal.ZERO);
                statistics.setChannelAloneRate(BigDecimal.ZERO);
                statistics.setChannelName("");
            }
            break;
            case CHANNEL: {
                //查询
                ChannelSettlementConfig info = channelSettlementConfigService.getById(one.getChannelId());
                BigDecimal invoiceRate = info.getInvoiceRate();
                BigDecimal fixInvoiceRate =BigDecimal.ONE.add(invoiceRate);
                Assert.notNull(info, "渠道配置信息不存在,请重新选择");
                info.buildConfigBean();
                List<ChannelSettlementConfig.Config> configs = info.getConfigs();
                Optional<ChannelSettlementConfig.Config> optional = configs.stream().filter(config -> {
                    if (config.getPlatform().contains(statistics.getPlatformCode()) && config.getInsuranceCompany().equals(config.getInsuranceCompany())) {
                        return true;
                    }
                    return false;
                }).findFirst();                Assert.isTrue(optional.isPresent(), "未配置保司{},平台{}", statistics.getInsuranceCompany(), statistics.getPlatformCode());
                ChannelSettlementConfig.Config config = optional.get();
                BigDecimal rate =  config.getFixRate();
                Assert.notNull(rate, "渠道{}保司{},平台{}固定费率不存在",info.getChannel(), statistics.getInsuranceCompany(), statistics.getPlatform());
                statistics.setChannelFeeRate(rate);
                statistics.setChannelFee(statistics.getExTaxPremium().divide(fixInvoiceRate, RoundingMode.CEILING).multiply(rate).setScale(2, RoundingMode.HALF_UP));
                statistics.setChannelAloneFee(BigDecimal.ZERO);
                statistics.setChannelAloneRate(BigDecimal.ZERO);
                statistics.setChannelName(info.getChannel());
                statistics.setSalesmanFeeRate(BigDecimal.ZERO);
                statistics.setSalesmanFee(BigDecimal.ZERO);
                statistics.setSalesmanId("");
                statistics.setSalesmanName("");
            }
            break;
        }
        updateById(statistics);
    }
}
